﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace Nhom16_PTTKHTTT_12HCB
{
    public partial class frmThem_Sua_NCC : Form
    {
        public delegate void ThemMoi();
        public delegate void CapNhat();

        public event ThemMoi KhiThemMoiNCC = null;
        public event CapNhat KhiCapNhatNCC = null;

        public string Action = "";
        public string ID = null;

        NCC_BUS ncc_bus;
        NCC ncc_dto;
        public frmThem_Sua_NCC()
        {
            InitializeComponent();
        }

        private void frmThem_Sua_NCC_Load(object sender, EventArgs e)
        {
            //load loai nguyen vat lieu vao combobox nguyen vat lieu
            //LoadComboboxLoaiNVL();
            ////
            //set man hinh luc loadform
            SetBounds(Screen.GetWorkingArea(this).Width / 2, Screen.GetWorkingArea(this).Height / 2, Width, Height);

            //them loai nguyen vat lieu
            if (Action.Equals("Insert"))
            {
                ncc_bus = new NCC_BUS();
                txtMaNCC.Text = ncc_bus.MaNCCTang();
            }
            //cap nhat scah
            if (Action.Equals("Update"))
            {
                ncc_bus = new NCC_BUS();
                DataTable ncc = new DataTable();

                ncc = ncc_bus.DanhSachNCC_Ma(ID);

                txtMaNCC.Text = ncc.Rows[0]["MaNCC"].ToString();
                txtTenNCC.Text = ncc.Rows[0]["TenNCC"].ToString();
                txtDiaChi.Text = ncc.Rows[0]["DiaChi"].ToString();
                txtDienThoai.Text = ncc.Rows[0]["DienThoai"].ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            //if (txtTenNVL.Text == "")
            //{
            //    MessageBox.Show("Chưa nhập tên nguyên vật liệu");
            //    txtTenNVL.Select();
            //    return;
            //}
            ncc_bus = new NCC_BUS();
            ncc_dto = new NCC();
            ncc_dto.MaNCC = txtMaNCC.Text;
            ncc_dto.TenNCC = txtTenNCC.Text;
            ncc_dto.DiaChi = txtDiaChi.Text;
            ncc_dto.DienThoai = txtDienThoai.Text;
            ////them moi
            
            if (Action.Equals("Insert"))
            {
                if (ncc_bus.ThemNCC(ncc_dto) == true)
                {
                    MessageBox.Show("Thêm thành công", "Thống báo");
                    if (KhiThemMoiNCC != null)
                    {
                        KhiThemMoiNCC();
                        txtMaNCC.Text = ncc_bus.MaNCCTang();
                    }
                }
                else
                    MessageBox.Show("Thêm thất bại", "Thông báo");
            }
            if (Action.Equals("Update"))
            {
                if (ncc_bus.CapNhatNCC(ncc_dto) == true)
                {
                    MessageBox.Show("Cập nhật thành công", "Thống báo");
                    if (KhiCapNhatNCC != null)
                    {
                        KhiCapNhatNCC();
                        this.Close();
                    }
                }
                else
                    MessageBox.Show("Cập nhật thất bại", "Thông báo");
            }
        }
    }
}
